﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Tests.PresentationModel
{
    [TestClass]
    public class PresentationTransferObjectTests
    {
        private TestPTO _pto;

        [TestInitialize]
        public void Setup()
        {
            this._pto = new TestPTO();
        }

        [TestMethod]
        public void NotifyPropertyChangedTest_RaisedOnNotify()
        {
            bool eventRaised = false;
            bool propertyNameValid = false;

            this._pto.PropertyChanged += (o, e) => { eventRaised = true; propertyNameValid = e.PropertyName == "prop"; };
            this._pto.RaiseNotifyPropertyChanged("prop");

            Assert.IsTrue(eventRaised);
            Assert.IsTrue(propertyNameValid);
        }

        [TestMethod]
        public void GetPropertyValueTest_NotSet()
        {
            //the Name property on the TestPTO class uses GetPropertyValue<string> - since this has not been set yet then it should return null 
            //(default for string)
            Assert.IsNull(this._pto.Name);
        }

        [TestMethod]
        public void GetPropertyValueTest_Set_CheckValue()
        {
            this._pto.Name = "name";
            Assert.AreEqual("name", this._pto.Name);
        }

        [TestMethod]
        public void GetPropertyValueTest_Set_CheckPropertyChangedRaised()
        {
            bool eventRaised = false;
            bool propertyNameValid = false;

            this._pto.PropertyChanged += (o, e) => { eventRaised = true; propertyNameValid = e.PropertyName == "Name"; };
            this._pto.Name = "newName";

            Assert.IsTrue(eventRaised);
            Assert.IsTrue(propertyNameValid);
        }

        [TestMethod]
        public void GetPropertyValueTest_Set_SameValue_CheckPropertyChangedNotRaised()
        {
            bool eventRaised = false;
            this._pto.PropertyChanged += (o, e) => { eventRaised = true; };

            //name is already null so setting it to the same value should not raise the PropertyChanged event
            this._pto.Name = null;

            Assert.IsFalse(eventRaised);
        }

        private class TestPTO : PresentationTransferObject
        {
            public void RaiseNotifyPropertyChanged(string propertyName)
            {
                this.NotifyPropertyChanged(propertyName);
            }

            public string Name
            {
                get { return this.GetPropertyValue<string>("Name"); }
                set { this.SetPropertyValue("Name", value); }
            }
        }
    }
}
